<template>
  <!-- 策略组合 -->
  <div class="combination-page">
    <el-form ref="dateForm" :inline="true" label-width="80px" :model="dateForm" class="demo-form-inline">
      <el-form-item label="方案名称" prop="portfolioName">
        <el-input v-model="dateForm.portfolioName" clearable placeholder="请输入方案名称" style="width: 260px;"></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 20px">
        <el-button class="bg-green" @click="getList">查询</el-button>
        <el-button type="danger" @click="addHandle">添加</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      size="mini"
      :header-cell-style="{background:'#dcedff',color:'#32639d'}"
      style="width: 100%;"
    >
      <el-table-column type="index" label="序号" align="center" width="60" fixed></el-table-column>
      <el-table-column prop="portfolioName" label="方案名称" min-width="160" fixed show-overflow-tooltip>
        <template slot-scope="scope">
          <span style="color: #4f95dd;cursor: pointer;" @click="netWorthHandle(scope.row)">{{ scope.row.portfolioName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="portfolioInitMoney" label="最新规模(万元)" align="center" min-width="100"></el-table-column>
      <el-table-column prop="makeTime" label="成立日期" align="center" min-width="90">
        <template slot-scope="{row}">
          <span>{{ row.makeTime | parseTime }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="portfolioNetWorthExtendBeans.0.incomeRate" label="今年以来收益率" align="center" min-width="130">
        <template slot-scope="{row}">
          <span v-if="row.portfolioNetWorthExtendBeans[0]&&row.portfolioNetWorthExtendBeans[0].incomeRate" :class="row.portfolioNetWorthExtendBeans[0].incomeRate>=0?'positiveFc':'negativeFc'">
            {{ row.portfolioNetWorthExtendBeans[0].incomeRate | toFixed2 }}%
          </span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="portfolioNetWorthExtendBeans.1.maxDownRate" label="近1月最大回撤" align="center" min-width="100">
        <template slot-scope="{row}">
          <span v-if="row.portfolioNetWorthExtendBeans[0]&&row.portfolioNetWorthExtendBeans[1].maxDownRate">
            {{ row.portfolioNetWorthExtendBeans[1].maxDownRate | toFixed2 }}%
          </span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="portfolioNetWorthExtendBeans.2.maxDownRate" label="近3月最大回撤" align="center" min-width="100">
        <template slot-scope="{row}">
          <span v-if="row.portfolioNetWorthExtendBeans[0]&&row.portfolioNetWorthExtendBeans[2].maxDownRate">
            {{ row.portfolioNetWorthExtendBeans[2].maxDownRate | toFixed2 }}%
          </span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="portfolioNetWorthExtendBeans.3.maxDownRate" label="近6月最大回撤" align="center" min-width="100">
        <template slot-scope="{row}">
          <span v-if="row.portfolioNetWorthExtendBeans[0]&&row.portfolioNetWorthExtendBeans[3].maxDownRate">
            {{ row.portfolioNetWorthExtendBeans[3].maxDownRate | toFixed2 }}%
          </span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="portfolioNetWorthExtendBeans.0.maxDownRate" label="今年以来最大回撤" align="center" min-width="116">
        <template slot-scope="{row}">
          <span v-if="row.portfolioNetWorthExtendBeans[0]&&row.portfolioNetWorthExtendBeans[0].maxDownRate">
            {{ row.portfolioNetWorthExtendBeans[0].maxDownRate | toFixed2 }}%
          </span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="portfolioNetWorthExtendBeans.99.maxDownRate" label="最大回撤" align="center">
        <template slot-scope="{row}">
          <span v-if="row.portfolioNetWorthExtendBeans[99].maxDownRate">
            {{ row.portfolioNetWorthExtendBeans[99].maxDownRate }}%
          </span>
          <span v-else>--</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column prop="investChannel" label="夏普比率" align="center"></el-table-column> -->
      <el-table-column prop="portfolioInitMoney" label="期初规模(万元)" align="center" min-width="100"></el-table-column>
      <!-- <el-table-column prop="investChannel" label="调仓次数" width="80" align="center"></el-table-column>
      <el-table-column prop="investChannel" label="最新调仓日期" align="center"></el-table-column> -->
      <el-table-column prop="createdby" label="创建人" align="center" min-width="80"></el-table-column>
      <el-table-column prop="updatedTime" label="更新时间" align="center" min-width="90">
        <template slot-scope="{row}">
          <span>{{ row.createdTime | parseTime }}</span>
        </template> 
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="170">
        <template slot-scope="{row}">
          <el-button class="filter-item" size="mini" type="primary" icon="el-icon-edit" @click="addOrUpdateHandle(row)">
            编辑
          </el-button>
          <el-button class="filter-item" size="mini" type="primary" icon="el-icon-delete" @click="deleteHandle(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageCurrent" :limit.sync="listQuery.limit" @pagination="getList" />

    <updateOpen v-if="updateVisible" ref="updateOpen" @closeOpen="getList"></updateOpen>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { portfolioList, deleteStrategyPortfolioById } from '@/api/combination'
import { mapGetters } from 'vuex'
import updateOpen from './updateOpen.vue'

export default {
  name: 'Combination',
  components: { Pagination, updateOpen },
  data() {
    return {
      listLoading: false,
      total: 0,
      dateForm: {},
      list: [],
      listQuery: {
        pageCurrent: 1,
        limit: 10
      },
      updateVisible: false
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    this.getList()
  },
  methods: {
    addOrUpdateHandle(row) {
      this.updateVisible = true
      this.$nextTick(() => {
        this.$refs.updateOpen.init(row)
      })
    },
    deleteHandle(row) {
      this.$confirm(`确认删除方案名称【${row.portfolioName}】此条数据？`, '提示', {
        type: 'warning'
      }).then(_ => {
        deleteStrategyPortfolioById({
          portfolioId: row.portfolioId
        }).then(res => {
          if(res.code == 0) {
            this.$notify({
              type: 'success',
              message: '删除成功',
              duration: 2000
            })
            this.getList()
          }
        })
      }).catch(_ => {})
    },
    netWorthHandle(row) {
      this.$router.push({
        path: '/strategyComb/optimize/netWorthAnalysis',
        query: {
          id: row.portfolioStrategyId,
          userId: this.userId,
          strategyType: row.portfolioModelId, // 1 均权，2均值方差 3风险平价
          portfolioId: row.portfolioId
        }
      })
    },
    resetForm() {
      this.$refs.dateForm.resetFields()
    },
    addHandle() {
      this.$router.push('/strategyComb/optimize/add')
    },
    getList() {
      this.listLoading = true
      portfolioList({
        userId: this.userId+'',
        ...this.dateForm,
        ...this.listQuery
      }).then(res => {
        this.listLoading = false
        this.list = res.body.list
        this.total = res.body.total
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
